package com.yaorange.module.system.controller;

import com.yaorange.module.system.service.SysRoleService;
import com.yaorange.module.vo.SysRolePageVo;
import com.yaorange.pojo.SysRole;
import com.yaorange.response.PageResult;
import com.yaorange.response.Result;
import com.yaorange.response.enums.ResultCode;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 15332
 * @version 1.0
 * @date 2025/6/16 19:26
 * @description 角色控制器
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
  @Autowired
  private SysRoleService service;

  /**
   * 查询角色信息
   * @param rolePageVo
   * @return
   */
  @GetMapping
  public Result<PageResult<SysRole>> queryByCondition(SysRolePageVo rolePageVo) {
    PageResult<SysRole> pageResult = service.query(rolePageVo);
    return Result.of(ResultCode.SUCCESS, pageResult);
  }

  /**
   * 根据ID查询
   * @param id
   * @return
   */
  @GetMapping("/{id}")
  public Result<SysRole> selectById(@PathVariable(value = "id") Long id){
    SysRole sysRole = service.select(id);
    return Result.of(ResultCode.SUCCESS,sysRole);
  }

  /**
   * 新增
   * @return
   */
  @PostMapping
  public Result<?> addRole(@Valid @RequestBody SysRole role){
    service.insert(role);
    return Result.of(ResultCode.SUCCESS);
  }

  /**
   * 修改
   * @return
   */
  @PutMapping
  public Result<?> updateRole(@Valid @RequestBody SysRole role){
    service.update(role);
    return Result.of(ResultCode.SUCCESS);
  }

  /**
   * 删除
   * @param id
   * @return
   */
  @DeleteMapping("/{id}")
  public Result<?> deleteRole(@PathVariable(value = "id") Long id){
    service.delete(id);
    return Result.of(ResultCode.SUCCESS);
  }

  /**
   * 根据角色列表批量删除角色
   * @return
   */
  @DeleteMapping
  public Result<?> deleteRoles(@RequestBody List<Long> ids){
    // 批量删除角色
    service.delRoles(ids);
    return Result.of(ResultCode.SUCCESS);
  }

  /**
   * 为角色指定权限
   */
  @PostMapping("/{id}/perm")
  public Result<?> getPermission(@RequestBody List<Long> ids,@PathVariable("id") Long id){
    service.getPerm(id,ids);
    return Result.success();
  }

}
